#include "debug.h"
#include <SDL/SDL_ttf.h>
#include <tchar.h>
#include "Elements\\Label.h"

using namespace SDL::UTILS;

NS_BEGIN(SDL)
NS_BEGIN(ELEMENTS)

#pragma region CTOR & DTOR

Label::Label(Font* _font, Color const& _foreground,
			 bool _deleteFont, Color const& _background)
	: AElement(), m_text(L""), m_font(_font), m_foreground(_foreground),
	m_deleteFont(_deleteFont), m_background(_background)
{
	CTOR("Label", this);
}

Label::Label(Label const& _label) : AElement(*((AElement*)&_label))
{
	CTOR("Label", this);
	*this = _label;
}

Label::~Label()
{
	DTOR("Label", this);
	if (m_deleteFont == true)
		delete m_font;
}

#pragma endregion
#pragma region Operators

Label& Label::operator=(Label const& _label)
{
	if (this != &_label)
	{
		*((AElement*)this) = *((AElement*)&_label);
		m_text = _label.m_text;
		m_font = _label.m_font;
		m_foreground = _label.m_foreground;
		m_background = _label.m_background;
		m_deleteFont = false;
	}
	return (*this);
}

bool Label::operator==(Label const& _label) const
{
	if (m_text != _label.m_text || m_foreground != _label.m_foreground ||
		m_background != _label.m_background)
		return (false);
	if (m_font == NULL || _label.m_font == NULL)
	{
		if (m_font != _label.m_font)
			return (false);
	}
	else
	{
		if (m_font->GetFont() != _label.m_font->GetFont())
			return (false);
	}
	return (true);
}

bool Label::operator!=(Label const& _label) const
{
	return (!(*this == _label));
}

#pragma endregion

bool Label::Draw(SDL_Surface* _screen)
{
	if (m_hidden == false)
	{
		if (m_surface != NULL)
			m_surface->BlitSurface(NULL, _screen, &m_position);
		else
			return (false);
	}
	return (true);
}

void Label::SetPosition(Sint16 _x, Sint16 _y)
{
	m_position.x = _x;
	m_position.y = _y;
}

void Label::SetPosition(Rect const& _position)
{
	m_position = _position;
}

void Label::Move(Sint16 _offsetX, Sint16 _offsetY)
{
	m_position.SetX(m_position.GetX() + _offsetX);
	m_position.SetY(m_position.GetY() + _offsetY);
}

void Label::Move(Rect const& _offset)
{
	m_position.SetX(m_position.GetX() + _offset.GetX());
	m_position.SetY(m_position.GetY() + _offset.GetY());
}

void Label::Render(std::wstring const& _text, tRenderSolidBlended _render)
{
	if (m_font != NULL)
	{
		m_text = _text;
		SDL_Surface* s =
			_render(m_font->GetFont(), (Uint16 const*)m_text.c_str(),
					m_foreground.GetColor());
		if (s == NULL)
			return ;
		if (m_surface == NULL)
			m_surface = new Surface(s);
		else
			m_surface->SetSurface(s);
	}
}

void Label::Render(std::wstring const& _text, tRenderShaded _render)
{
	if (m_font != NULL)
	{
		m_text = _text;
		SDL_Surface* s = _render(m_font->GetFont(), (Uint16 const*)m_text.c_str(),
								 m_foreground.GetColor(), m_background.GetColor());
		if (s == NULL)
			return ;
		if (m_surface == NULL)
			m_surface = new Surface(s);
		else
			m_surface->SetSurface(s);
	}
}

void Label::Render(std::string const& _text, tRenderSolidBlended _render)
{
	this->Render(Font::StringToWString(_text), _render);
}

void Label::Render(std::string const& _text, tRenderShaded _render)
{
	this->Render(Font::StringToWString(_text), _render);
}

#pragma region Getters & Setters

std::wstring const& Label::GetText() const
{
	return (m_text);
}

Font* Label::GetFont() const
{
	return (m_font);
}

void Label::SetFont(Font* _font)
{
	m_font = _font;
}

Color const& Label::GetForeground() const
{
	return (m_foreground);
}

void Label::SetForeground(Color const& _foreground)
{
	m_foreground = _foreground;
}

bool Label::GetDeleteFont() const
{
	return (m_deleteFont);
}

void Label::SetDeleteFont(bool _value)
{
	m_deleteFont = _value;
}

Color const& Label::GetBackground() const
{
	return (m_background);
}

void Label::SetBackground(Color const& _background)
{
	m_background = _background;
}

#pragma endregion

NS_END(ELEMENTS)
NS_END(SDL)
